import { defineStore } from 'pinia';
import {storage} from "@/utils"
import Vue from "./../main"
/**
 *  defineStore 第一个参数，实际项目中建议定义成枚举类型
 * 实际项目中，定义不同的store用不同的文件分割出来，index中用来到处store 和基础的数据 useStore 
 */

function getDict(params) {
	return new Promise(function(resolve, reject){
		Vue.http.get('global.getDict',{ params }).then(res => {
			resolve(res.data);
		}).catch(err => reject(err));
	})
}

function getHttp(url, params) {
	return new Promise(function(resolve, reject){
		Vue.http.get(url, {params}).then(res => {
			let arr = res.data.map(i => {
				i['label'] = i.name || i.selectName;
				i['value'] = i.id;
				return i;
			})
			resolve(arr);
		}).catch(err => reject(err));
	})
}

export const globalStore = defineStore("GLOBAL", {
	state: () => ({
		alarmNumber: 0,
		totalStatistics: {},
		ElNotification: [],

		userInfo: {},
		token: '',
		authData: {
			headerMenu: [
				
			]
		},

		// select
		alarmSelect: [],
		citySelect: [],
		classificationSelect: [],
		treeOrganizationSelect: [],
		roleSelect: [],
		organizationSelect: [],
		newCitySelect: [],
		newOrganizationSelect: [],
		cameraBindSelect: [],
		cameraSearchSelect: [],
		alarmVehicleSelect: [],
	}),
	getters: {
		numElNotification: (state) => state.ElNotification.length,
		isTourist: (state) => state.userInfo?.role != 2,					// 游客的权限
		isAdmin: (state) => state.userInfo?.role == 0,			// 只有admin账号有操作的权限
	},
	// 修改方法
	actions: {
		/**
		 * @param {Object} ElNotification 
		 * {id: 告警id, notify: notify弹框对象}
		 */
		setElNotification(ElNotification) {
			this.ElNotification.push(ElNotification);
		},
		deleteElNotification(ElNotification){
			this.ElNotification = this.ElNotification.filter(i => i.id !=ElNotification.id);
		},
		showNotify(id) {
			return this.ElNotification.every(i => i.id != id)
		},


		setUserInfo(userInfo) {
			this.userInfo = userInfo;
		},
		setToken(token) {
			this.token = token;
		},
		logout(){
			this.userInfo = null
			this.token = null
			storage.clearAll();
		},
		setAlarmNumber(alarmNumber) {
			this.alarmNumber = alarmNumber;
		},
		setTotalStatistics(totalStatistics) {
			this.totalStatistics = totalStatistics;
		},

		getAlarmSelect(){
			return new Promise((resolve, reject) =>{
				if (this.alarmSelect.length > 0) {
					resolve(this.alarmSelect)
				}else{
					Vue.http.get('archiveData.dictDetail', {params: {name: 'alarm_type_status'}}).then(res=>{
						let arr = res.data.map(i => {
							if (i.value == 'PHONE') {
								i['stateName'] = 'Phone';
								i['alarmId'] = 1;
								i['backgroundColor'] = 'rgba(251, 57, 60, 1)';
								i['icon'] = require("@/assets/image/index/list_ico_phone.png");
								i['alarmImg'] = require("@/assets/image/index/list_ico_level1.png");
							};
							if (i.value == "WHEELCHAIR") {
								i['stateName'] = 'Wheelchair';
								i['alarmId'] = 3;
								i['backgroundColor'] = 'rgba(246, 187, 44, 1)';
								i['icon'] = require("@/assets/image/index/list_ico_wheelchair.png");
								i['alarmImg'] = require("@/assets/image/index/list_ico_level3.png");
							};
							if (i.value == 'SMOKE') {
								i['stateName'] = 'Smoke';
								i['alarmId'] = 2;
								i['backgroundColor'] = 'rgba(248, 120, 34, 1)';
								i['icon'] = require("@/assets/image/index/list_ico_somke.png");
								i['alarmImg'] = require("@/assets/image/index/list_ico_level2.png");
							};
							if (i.value == 'OCCLUSION') {
								i['stateName'] = 'Occlusion';
								i['alarmId'] = 4;
								i['backgroundColor'] = 'rgba(184, 111, 222, 1)';
								i['icon'] = require("@/assets/image/index/list_ico_occlusion.png");
								i['alarmImg'] = require("@/assets/image/index/list_ico_level4.png");
							};
							return i;
						});
						this.alarmSelect = arr;
						resolve(arr)
					}).catch(err=>reject(err));
				}
			})
		},
		async getSelect(name, params) {
			let select = [];
			if (!params && this[name]?.length > 0) {
				return this[name]
			}else{
				let selectList = [
					{label: 'classificationSelect', value: 'ambulance_classification'},
					{label: 'roleSelect', value: 'user_role'},
					
					{label: 'cameraSelect', value: 'http', url: 'global.getCameraSelect'},
					{label: 'citySelect', value: 'http', url: 'global.getCitySelect'},
					{label: 'newCitySelect', value: 'http', url: 'global.getCitySelectByHospitalId'},
					{label: 'treeOrganizationSelect', value: 'http', url: 'global.getOrganizationMultipleList'},
					{label: 'organizationSelect', value: 'http', url: 'global.getHospitalSelect'},
					{label: 'newOrganizationSelect', value: 'http', url: 'global.getAmbulanceHospitalSelect'},
					{label: 'cameraBindSelect', value: 'http', url: 'global.getDeviceSelect'},
					{label: 'cameraSearchSelect', value: 'http', url: 'global.getAmbulanceCameraSelect'},
					{label: 'alarmVehicleSelect', value: 'http', url: 'global.getAlarmAmbulanceSelect'},
					
				]
				let item = selectList.find(i => i.label == name);
				let param = {
					...params
				};
				if (item?.value != 'http') {
					param['name'] = item.value
					select = item ? await getDict(param) : [];
				}else{
					select = item ? await getHttp(item.url, param) : [];
				}
				this[name] = select;
				return select;
			}
		},
	},
})